package site.laodengtou.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import jxl.Sheet;
import jxl.Workbook;
import site.laodengtou.pojo.Question;
import site.laodengtou.pojo.QuestionVo;
import site.laodengtou.pojo.Student;
import site.laodengtou.pojo.Teacher;
import site.laodengtou.pojo.Works;
import site.laodengtou.pojo.WorksVo;
import site.laodengtou.pojo.resultquestion;
import site.laodengtou.service.TeacherService;
import site.laodengtou.util.Msg;
import site.laodengtou.util.PagesMap;

@Controller
public class TeacherController {

	@Autowired
	TeacherService teacherService;
	
	@RequestMapping(value="/GPdaoRu",method=RequestMethod.POST)
	public void importFile(MultipartFile file) throws Exception {
		System.out.println("开始导入学生信息");
	    // 1.获取用户上传的文件
	    Workbook workbook = Workbook.getWorkbook(file.getInputStream());
	    // 2.获取工作簿sheet
	    Sheet sheet = workbook.getSheet(0);
	    // 3.获取总行数
	    int rows = sheet.getRows();
	    for (int i = 1; i < rows; i++) {
	        Student stu = new Student();
	        stu.setKsh(sheet.getCell(0, i).getContents());
	        stu.setName(sheet.getCell(1, i).getContents());
	        stu.setCarnumer(sheet.getCell(2, i).getContents());
	        String cellphone=sheet.getCell(2, i).getContents();
	        stu.setPassword(cellphone.substring(cellphone.length()-6));
	        // 4.添加到数据库中
	        teacherService.inputInformation(stu);
	    }
	    // 5.关闭资源
	    workbook.close();
	    JOptionPane.showMessageDialog(null, "导入成功");
	}
	
	
	//教师登录
	@RequestMapping(value="/tealogin",method=RequestMethod.POST)
	public String tealogin(Teacher tea) throws Exception {
	    
		List<Teacher> list = teacherService.tealogin(tea);
		if(list.size()==1) {
			return "indexTeacher";
		}else {
			return "loginTeacher";
		}
	}
	
	
	//添加试题
	@RequestMapping(value="/addItem",method=RequestMethod.POST)
	public void addItem(Question question){
	    System.out.println(question);
		teacherService.addItem(question);
		JOptionPane.showMessageDialog(null, "添加成功");
	}
	
	//老师发布考试
	@RequestMapping(value="/releaseTest",method=RequestMethod.POST)
	public void releaseTest(WorksVo works) throws Exception{
	    //先插入数据，然后定时修改数据信息
		
		String starttime = works.getStarttime()+" "+works.getStime();
		String endtime = works.getEndtime()+" "+works.getEtime();
		Date datestart = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(starttime);
		Date dateend = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(endtime);
		Date now = new Date();
		Works w = new Works();
		
		w.setState("正在考试");
		if(datestart.before(now)) {
			w.setState("已结束");
		}
		if(dateend.before(now)) {
			w.setState("未开启");
		}
		w.setEndtime(dateend);
		w.setStarttime(datestart);
		w.setWorktitle(works.getWorktitle());
		w.setWorktype(works.getWorktype());
		teacherService.releaseTest(w);		
		
		JOptionPane.showMessageDialog(null, "发布成功");
	}
	
	//查询出所有发布的考试
	@RequestMapping(value="/selectExamination",method=RequestMethod.POST)
	@ResponseBody
	public Msg selectExamination(int pn){
		
		PageHelper.startPage(10, pn);
		List<Works> list = teacherService.selectExamination();
		PageInfo pageInfo = new PageInfo(list,pn);
		return Msg.success().add("pageInfo", pageInfo);
	}
	
	
	//查询出某场考试的所有考生
	@RequestMapping(value="/selectStudentById",method=RequestMethod.POST)
	@ResponseBody
	public Msg selectStudentById(int worksId){
		List<Student> list = teacherService.selectStudentById(worksId);
		return Msg.success().add("list", list);
	}
	
	//查询出某场考试的考生的考卷
	@RequestMapping(value="/selectStupaper",method=RequestMethod.POST)
	@ResponseBody
	public Msg selectStupaper(String ksh,int worksId){
		 List<QuestionVo> list = teacherService.selectStupaper(ksh, worksId);
		return Msg.success().add("list", list);
	}
	
	//改试题
	@RequestMapping(value="/changeTheTopic",method=RequestMethod.POST)
	@ResponseBody
	public Msg changeTheTopic(String ksh,int worksId,int questionid,int grade){
		resultquestion rq = new resultquestion();
		rq.setKsh(ksh);
		rq.setWorkid(worksId);
		rq.setGrade(grade);
		rq.setQuestionid(questionid);
		teacherService.changeTheTopic(rq);
		return Msg.success();
	}
}
